package com.lagou.api;

import org.apache.kudu.ColumnSchema;
import org.apache.kudu.Schema;
import org.apache.kudu.Type;
import org.apache.kudu.client.CreateTableOptions;
import org.apache.kudu.client.KuduClient;
import org.apache.kudu.client.KuduException;

import java.util.ArrayList;
import java.util.concurrent.ScheduledExecutorService;

/*
kudu建表几个必须：
（1）必须指定表连接到的master节点主机名
（2）必须定义schema
（3）必须指定副本数量、分区策略和数量
 */
public class CreateTable {
    public static void main(String[] args) {

        //1）必须指定表连接到的master节点主机名
        String masterAddresses="zb26105";
        KuduClient.KuduClientBuilder kuduClientBuilder = new KuduClient.KuduClientBuilder(masterAddresses);
        KuduClient client = kuduClientBuilder.build();

        String tableName = "flinktable";
        //2）必须定义schema
        ArrayList<ColumnSchema> columnSchemas = new ArrayList<ColumnSchema>();
        ColumnSchema id = new ColumnSchema.ColumnSchemaBuilder("id", Type.STRING).key(true).build();
        ColumnSchema name = new ColumnSchema.ColumnSchemaBuilder("name", Type.STRING).key(false).build();
        ColumnSchema age = new ColumnSchema.ColumnSchemaBuilder("age", Type.INT32).key(false).build();

        columnSchemas.add(id);
        columnSchemas.add(name);
        columnSchemas.add(age);

        Schema schema = new Schema(columnSchemas);

        //3）必须指定副本数量、分区策略和数量

        CreateTableOptions options = new CreateTableOptions();
        options.setNumReplicas(1);
        ArrayList<String> columns = new ArrayList<String>();
        columns.add("id");
        options.addHashPartitions(columns,3);

        try {
            client.createTable(tableName,schema,options);
        } catch (KuduException e) {
            e.printStackTrace();
        }finally {
            try {
                client.close();
            } catch (KuduException e) {
                e.printStackTrace();
            }
        }


    }
}
